package com.wsx.rpc.metrics.meters;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author wangshuaixin
 * @Title: com.wsx.rpc.metrics.meters.MetersTest
 * @Description: 用来度量某个时间段的平均处理次数（request per second），每1、5、15分钟的TPS
 * @date 2018/11/26
 */
public class MetersTest {


    private static final MetricRegistry REGISTRY = new MetricRegistry();


    private static ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(REGISTRY).build();

    private static final Meter METER = REGISTRY.meter(MetricRegistry.name(MetersTest.class, "quest"));

    private static void request() {
        METER.mark();
    }

    public static void main(String[] args) {
        consoleReporter.start(3, TimeUnit.SECONDS);

        boolean go = true;
        int num = 1;
        Random random = new Random();
        while (go) {
            if (num >= 1000) {
                go = false;
            }

            request();
            num ++;
            try {
                Thread.sleep(random.nextInt(100));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
